<template>
  <div>
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="部门名称">
        <el-input v-model="queryForm.deptName" placeholder="部门名称" clearable />
      </el-form-item>
      <el-form-item label="负责人">
        <el-input v-model="queryForm.leader" placeholder="请输入负责人" clearable />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="queryForm.phone" placeholder="请输入手机号" clearable />
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="queryForm.email" placeholder="请输入邮箱" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list"
    border
    style="width: 100%; min-width: 800px; border-radius: 12px; overflow: hidden; box-shadow: 0px 4px 20px rgba(0, 0, 0, 0.1);"
    :header-cell-style="{ backgroundImage: 'linear-gradient(to right, #f0f9ff, #e5eaff)', color: '#333', fontWeight: 'bold' }"
    :cell-style="{ padding: '12px 8px' }"
    >
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="创建时间" align="center" prop="createTime" sortable />
      <el-table-column label="操作" align="center">
        <template #default="scope"  >
          <div class="button-group">
          <el-button link type="primary"  class="btn-design" icon="Edit" @click="handleUpdate(scope.row.deptId)">修改</el-button>
          <el-button link type="primary"  class="btn-delete" icon="Delete" @click="handleDelete(scope.row.deptId)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background layout="prev, pager, next" v-model:page-size="queryForm.pageSize" v-model:current-page="queryForm.pageNo" :total="total" @current-change="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog v-model="open" :title="title" width="500px" append-to-body :close-on-click-modal="false" :modal="false">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick, watch } from "vue";
import baseService from "@/service/baseService";
import { ElMessage, ElMessageBox } from "element-plus";
import { debounce } from "lodash-es";

// 查询参数
const queryForm = reactive({
  deptName: "",
  leader: "",
  phone: "",
  email: "",
  pageNo: 1,
  pageSize: 10
});


// 列表内容数量
const total = ref(0);
// 列表是否加载
const loading = ref(true);
// 列表返回值
const list = ref<any[]>([]);

// 表单实例
const formRef = ref();
// 是否打开弹出框
const open = ref(false);
// 弹出框标题
const title = ref("");
// 提交表单数据
const form = reactive({
  deptId: "",
  deptName: "",
  leader: "",
  phone: "",
  email: ""
});

// 表单验证
const rules = ref({
  deptName: [
    { required: true, message: "部门名称不能为空", trigger: "blur" },
    {
      pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_]+$/, // 正则表达式，用于校验中文、英文和数字
      message: "部门名称只能包含中文、英文和数字",
      trigger: "blur"
    }
  ],
  leader: [
    { required: true, message: "负责人不能为空", trigger: "blur" },
    {
      pattern: /^[\u4e00-\u9fa5a-zA-Z0-9_]+$/, // 正则表达式，用于校验中文、英文和数字
      message: "负责人只能包含中文、英文和数字",
      trigger: "blur"
    }
  ],
  phone: [
    { required: true, message: "手机号不能为空", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/, // 正则表达式，用于校验中国大陆的手机号码
      message: "请输入正确的手机号码",
      trigger: "blur"
    }
  ],
  email: [
    { required: true, message: "邮箱不能为空", trigger: "blur" },
    {
      pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/, // 简单的邮箱正则表达式
      message: "请输入正确的邮箱地址",
      trigger: "blur"
    }
  ]
});


/**
 * 查询列表
 */
const getList = () => {
  loading.value = true;
  baseService
    .get("/sysDept/list", queryForm)
    .then((res) => {
      loading.value = false;
      if (res.code === 200) {
        list.value = res.rows;
        total.value = res.total;
      } else {
        list.value = [];
      }
    })
    .catch(() => {
      loading.value = false;
    });
};

/**
 * 表单重置
 */
function resetQuery() {
  queryForm.deptName = "";
  queryForm.phone = "";
  queryForm.email = "";
  queryForm.leader = "";
  queryForm.pageNo = 1;
  queryForm.pageSize = 10;
  getList();
}

// 设置每个输入框的 `watch`，监听变化，并向后端请求数据
watch(() => queryForm.deptName, debounce(getList, 500));
watch(() => queryForm.phone, debounce(getList, 500));
watch(() => queryForm.email, debounce(getList, 500));
watch(() => queryForm.leader, debounce(getList, 500));
/**
 * 重置表单
 */
function resetForm() {
  form.deptId = "";
  form.deptName = "";
  form.leader = "";
  form.phone = "";
  form.email = "";

  nextTick(() => {
    formRef.value?.resetFields();
  });
}

/**
 * 新增按钮操作
 */
function handleAdd() {
  resetForm();
  open.value = true;
  title.value = "添加";
}

/**
 * 修改按钮操作
 * @param id 主键
 */
function handleUpdate(id: string) {
  open.value = true;
  title.value = "修改";
  baseService.get(`/sysDept/info/${id}`).then((res) => {
    if (res.code === 200) {
      Object.assign(form, res.data); // 动态更新表单数据
    }
  });
}

/**
 * 提交按钮
 */
 function submitForm() {
  formRef.value.validate((valid: boolean) => {
    if (!valid) return;
    baseService.post(`/sysDept/save`, form).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        open.value = false;
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

/**
 * 删除按钮操作
 * @param id 主键
 */
function handleDelete(id: any) {
  ElMessageBox.confirm("确认要删除当前项吗?", "提示").then(() => {
    baseService.delete(`/sysDept/delete`, id).then((res) => {
      if (res.code === 200) {
        ElMessage.success(res.msg);
        getList();
      } else {
        ElMessage.error(res.msg);
      }
    });
  });
}

// 初始化列表数据
getList();
</script>

<style scoped>
/* 表格整体样式 */
.el-table {
  background-color: #fff;
  font-size: 14px;
  transition: all 0.3s;
}

/* 表头样式 */
.el-table__header-wrapper th {
  background-image: linear-gradient(to right, #f0f9ff, #e5eaff);
  color: #333;
  font-weight: bold;
  border-bottom: 1px solid #ebeef5;
}

/* 行悬停效果 */
.el-table .el-table__body-wrapper tr:hover {
  background-color: #f0f9ff;
  transition: background-color 0.2s ease;
}

/* 状态标签样式 */
.tag-danger {
  background-color: #ff6b6b !important;
  color: #fff !important;
  border-radius: 4px;
  font-weight: 500;
}
.tag-active {
  background-color: #4caf50 !important;
  color: #fff !important;
  border-radius: 4px;
  font-weight: 500;
}

/* 按钮组样式 */
.button-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
}
.button-group .el-button {
  padding: 8px 12px;
  border-radius: 4px;
  font-weight: 500;
  min-width: 60px;
  transition: all 0.2s;
}
.btn-design {
  background-color: #3b82f6;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.btn-activate {
  background-color: #4caf50;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.btn-suspend {
  background-color: #ff9800;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.btn-view {
  background-color: #5c6bc0;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.btn-delete {
  background-color: #e57373;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.button-group .el-button:hover {
  opacity: 0.9;
  transform: scale(1.02);
}

/* 无信息提示样式 */
.empty-info {
  color: #999;
  font-size: 14px;
  text-align: center;
  margin: 0;
  font-weight: 500;
}
</style>